package com.briup.dom;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.io.File;
import java.util.List;
import java.util.Objects;

public class domTest2 {
    /**
     * 读取全部一级标签的class属性（类的全限定名）
     * 创建实例对象
     */
    @Test
    public void test() {
        try {
            File file = new File("D:\\training2\\idea_code\\Smart\\src\\main\\resources\\config.xml");
            SAXReader SAXReader = new SAXReader();
            Document read = SAXReader.read(file);

            Element rootElement = read.getRootElement();
            List<Element> elements1 = rootElement.elements();

            for (Element element : elements1) {
                //获取class属性
                Attribute attribute = element.attribute("class");
                if(Objects.nonNull(attribute)){
                    String value = attribute.getValue();
                    //获取字节码对象
                    Class<?> aClass = Class.forName(value);
                    //创建实例化对象
                    Object o = aClass.newInstance();
                    System.out.println("o = " + o);

                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
